package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.RagFlowFileService;
import org.springframework.web.bind.annotation.*;

/**
 * 文件控制器
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/file")
public class RagFlowFileController {
    private final RagFlowFileService ragFlowFileService;

    /**
     * 文件上传
     *
     * @param request 上传请求对象
     * @return 通用响应对象
     */
    @PostMapping("/upload")
    public R<Object> upload(@RequestBody FileUploadRequest request) {
        try {
            return R.ok(ragFlowFileService.upload(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 创建文件或文件夹
     *
     * @param request 创建请求对象
     * @return 通用响应对象
     */
    @PostMapping("/create")
    public R<Object> create(@RequestBody FileCreateRequest request) {
        try {
            return R.ok(ragFlowFileService.create(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取文件列表
     *
     * @param request 列表请求对象
     * @return 通用响应对象
     */
    @GetMapping("/list")
    public R<Object> list(FileListRequest request) {
        try {
            return R.ok(ragFlowFileService.list(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取根文件夹
     *
     * @return 通用响应对象
     */
    @GetMapping("/root_folder")
    public R<Object> getRootFolder() {
        try {
            return R.ok(ragFlowFileService.getRootFolder());
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取父文件夹
     *
     * @param fileId 文件或文件夹 ID
     * @return 通用响应对象
     */
    @GetMapping("/parent_folder")
    public R<Object> getParentFolder(@RequestParam String fileId) {
        try {
            return R.ok(ragFlowFileService.getParentFolder(fileId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取所有父文件夹
     *
     * @param fileId 文件或文件夹 ID
     * @return 通用响应对象
     */
    @GetMapping("/all_parent_folder")
    public R<Object> getAllParentFolders(@RequestParam String fileId) {
        try {
            return R.ok(ragFlowFileService.getAllParentFolders(fileId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除文件或文件夹
     *
     * @param request 删除请求对象
     * @return 通用响应对象
     */
    @PostMapping("/rm")
    public R<Object> rm(@RequestBody FileRmRequest request) {
        try {
            return R.ok(ragFlowFileService.rm(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 重命名文件或文件夹
     *
     * @param request 重命名请求对象
     * @return 通用响应对象
     */
    @PostMapping("/rename")
    public R<Object> rename(@RequestBody FileRenameRequest request) {
        try {
            return R.ok(ragFlowFileService.rename(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取文件
     *
     * @param fileId 文件 ID
     * @return 通用响应对象
     */
    @GetMapping("/get/{fileId}")
    public R<Object> get(@PathVariable String fileId) {
        try {
            return R.ok(ragFlowFileService.get(fileId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 移动文件或文件夹
     *
     * @param request 移动请求对象
     * @return 通用响应对象
     */
    @PostMapping("/mv")
    public R<Object> move(@RequestBody MoveRequest request) {
        try {
            return R.ok(ragFlowFileService.move(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
